//
//  HomePageViewController.m
//  TeamInformatization
//
//  Created by bigger on 2019/3/31.
//  Copyright © 2019年 SPIC. All rights reserved.
//

#import "HomePageViewController.h"
#import "HomePageViewCell.h"
#import "ControlCollectionViewCell.h"
#import "BannerView.h"
#import "StartWorkViewController.h"
#import "BeforeWorkListViewController.h"
#import "AllInvoiceViewController.h"


@interface HomePageViewController () <UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
// 视图
@property (nonatomic, strong) UITableView *homePageTableView;
// 轮播图
@property (nonatomic, strong) BannerView *homePageBannerView;
// 待办事项
@property (nonatomic, strong) SimpleLabel *todoLabel;
// 分类按钮
@property (nonatomic, strong) UICollectionView *controlCollectionView;

// 数据
@property (nonatomic, strong) NSMutableArray *tableViewDataArray;

@end

@implementation HomePageViewController

{
    // 8个按钮
    NSArray *_controlImageArr, *_controlTitleArr;
    
    // 提示线
    UIView *_indicateView;
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBarHidden = NO;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 初始化
    _controlImageArr = @[@"分组 21", @"分组 22", @"分组 23", @"分组 24", @"caozuopiao", @"分组 26", @"分组 25", @"分组 28", @"分组 33", @"gongzuopiao"];
    _controlTitleArr = @[@"班前会", @"工前会", @"班后会", @"安全活动", @"所有操作票", @"上岗到位", @"学习培训", @"在线考试", @"互动社区", @"所有工作票"];
    
    self.tableViewDataArray = [NSMutableArray arrayWithCapacity:0];
    
    // UI
    [self loadShowViews];
    
    // Data
    [self requestHomePageData];
}

// Data
- (void)requestHomePageData
{
    
    
    [self.homePageTableView reloadData];
}


//
- (void)loadShowViews
{
    self.homePageTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -StatusBarHeight, kScreenWidth, kScreenHeight - kSafeAreaBottomHeight - kTabbarHeight + StatusBarHeight) style:UITableViewStylePlain];
    [_homePageTableView registerClass:[HomePageViewCell class] forCellReuseIdentifier:@"HomePage"];
    _homePageTableView.delegate = self;
    _homePageTableView.dataSource = self;
    _homePageTableView.backgroundColor = [UIColor clearColor];
    _homePageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _homePageTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_homePageTableView];
    
    // headerView
    [self setHomePageHeaderView];
}

// tableViewHeaderView
- (void)setHomePageHeaderView
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 476 * kScaleWidth)];
    headerView.backgroundColor = kSetRGBColor(241, 241, 241, 1);
    
    // 轮播图
    self.homePageBannerView = [[BannerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 180 * kScaleWidth) andImageURLArray:@[] andplaceholderImage:@[@"分组 24", @"分组 25", @"分组 26"]];
    [headerView addSubview:_homePageBannerView];
    
    //待办事项
    UIView *todoView = [[UIView alloc] init];
    todoView.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:todoView];
    [todoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(180 * kScaleWidth);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 36 * kScaleWidth));
    }];
    
    SimpleLabel *promptLabel = [SimpleLabel initWithText:@"待办事项" TextColor:kTitleTextColor TextFont:[UIFont boldSystemFontOfSize:14] TextAlignment:NSTextAlignmentLeft];
    [todoView addSubview:promptLabel];
    [promptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15 * kScaleWidth);
        make.centerY.mas_equalTo(todoView);
    }];
    
    self.todoLabel = [SimpleLabel initWithText:@"您有 2 条新的任务" TextColor:kTitleTextColor TextFont:[UIFont systemFontOfSize:12] TextAlignment:NSTextAlignmentLeft];
    [todoView addSubview:_todoLabel];
    [_todoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(promptLabel.mas_right).offset(10 * kScaleWidth);
        make.centerY.mas_equalTo(todoView);
    }];
    
//    UIView *controlView = [[UIView alloc] init];
//    controlView.backgroundColor = [UIColor whiteColor];
//    [headerView addSubview:controlView];
//    [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(todoView.mas_bottom).offset(10 * kScaleWidth);
//        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 191 * kScaleWidth));
//    }];
    
    // 流水布局
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];      //创建流水布局
    flowLayout.itemSize = CGSizeMake(kScreenWidth / 5., 95 * kScaleWidth);      //设置每个格子的尺寸
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing = 0;        //设置每一行之间的间距
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.headerReferenceSize = CGSizeMake(0, 0);
    flowLayout.footerReferenceSize = CGSizeMake(0, 0);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    UICollectionView *controlCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 226 * kScaleWidth, kScreenWidth, 190 * kScaleWidth) collectionViewLayout:flowLayout];
    controlCollectionView.backgroundColor = [UIColor whiteColor];
    controlCollectionView.delegate = self;
    controlCollectionView.dataSource = self;
    controlCollectionView.scrollEnabled = NO;
    [controlCollectionView registerClass:[ControlCollectionViewCell class] forCellWithReuseIdentifier:@"ControlCell"];
    controlCollectionView.bounces = NO;
    // 横向滑动
    controlCollectionView.showsVerticalScrollIndicator = NO;
    controlCollectionView.showsHorizontalScrollIndicator = NO;
    [headerView addSubview:controlCollectionView];
    
    // 切换栏
    NSArray *titleArr = @[@"新闻中心", @"班组动态", @"专题活动", @"互动社区", @"上级文件"];
    for (int i = 0; i < 5; i++) {
        UIButton *changeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [changeButton setBackgroundColor:[UIColor whiteColor]];
        [changeButton setTitleColor:kSetRGBColor(102, 102, 102, 1) forState:UIControlStateNormal];
        [changeButton setTitleColor:kCommonRedColor forState:UIControlStateSelected];
        changeButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [changeButton setTitle:titleArr[i] forState:UIControlStateNormal];
        [changeButton addTarget:self action:@selector(changeShowAction:) forControlEvents:UIControlEventTouchUpInside];
        changeButton.selected = NO;
        changeButton.tag = 1000 + i;
        if (i == 0) {
            changeButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            changeButton.selected = YES;
        }
        [headerView addSubview:changeButton];
        [changeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(- 1 * kScaleWidth);
            make.left.mas_equalTo(kScreenWidth / 5. * i);
            make.size.mas_equalTo(CGSizeMake(kScreenWidth / 5., 48 * kScaleWidth));
        }];
    }
    
    _indicateView = [[UIView alloc] init];
    _indicateView.backgroundColor = kCommonRedColor;
    [headerView addSubview:_indicateView];
    [_indicateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-2 * kScaleWidth);
        make.left.mas_equalTo(10 * kScaleWidth);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth / 5. - 20 * kScaleWidth, 2 * kScaleWidth));
    }];
    
    
    _homePageTableView.tableHeaderView = headerView;
}

// 切换显示北容
- (void)changeShowAction:(UIButton *)sender
{
    for (int i = 1000; i < 1005; i++) {
        UIButton *button = [self.view viewWithTag:i];
        button.selected = NO;
        button.titleLabel.font = [UIFont systemFontOfSize:14];
    }
    
    sender.selected = YES;
    sender.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    
    
    [_indicateView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kScreenWidth / 5. * (sender.tag - 1000) + 10 * kScaleWidth);
    }];
    
    [_homePageTableView reloadData];
}


#pragma mark - CollectionViewDelegate/DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _controlTitleArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ControlCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ControlCell" forIndexPath:indexPath];
    
    [cell setShowViewCellWithTitle:_controlTitleArr[indexPath.row] ImageStr:_controlImageArr[indexPath.row]];
    
    return cell;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self indexWithRow:indexPath.row];
}


#pragma mark - TableViewDelegate/DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 94 * kScaleWidth;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    ActiveTeam *activeTeam = [[ActiveTeam alloc] init];
//    if (self.teamListArray.count != 0) {
//        activeTeam = self.teamListArray[indexPath.row];
//    }
    
    HomePageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomePage" forIndexPath:indexPath];
    if (!cell) {
        cell = [[HomePageViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HomePage"];
    }
    
    [cell setShowViewCellWithNewsData:nil];
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    ActiveTeam *activeTeam = [[ActiveTeam alloc] init];
//    if (self.teamListArray.count != 0) {
//        activeTeam = self.teamListArray[indexPath.row];
//    }
    
//    TeamDetailViewController *teamDetailVC = [[TeamDetailViewController alloc] init];
//    teamDetailVC.teamID = activeTeam.id1;
//    [self.navigationController pushViewController:teamDetailVC animated:YES];

}

#pragma mark - 点击collectionViewcell
-(void)indexWithRow:(NSInteger)row {
    switch (row) {
        case 0: {
            BeforeWorkListViewController *workVC = [BeforeWorkListViewController new];
            workVC.meetType = 1;
            workVC.vcTitle = @"班前会列表";
            [self.navigationController pushViewController:workVC animated:YES];
            break;
        }
        case 1: {
            BeforeWorkListViewController *workVC = [BeforeWorkListViewController new];
            workVC.meetType = 2;
            workVC.vcTitle = @"工前会列表";
            [self.navigationController pushViewController:workVC animated:YES];
            break;
        }
        case 2: {
            BeforeWorkListViewController *workVC = [BeforeWorkListViewController new];
            workVC.meetType = 3;
            workVC.vcTitle = @"班后会列表";
            [self.navigationController pushViewController:workVC animated:YES];
            break;
        }
        case 3: {
            
            break;
        }
        case 4: {
            
            break;
        }
        case 5: {
            StartWorkViewController *workVC = [StartWorkViewController new];
            [self.navigationController pushViewController:workVC animated:YES];
            break;
        }
        case 6: {
            ShowWebViewController *showWVC = [[ShowWebViewController alloc] init];
            showWVC.showUrl = @"http://kaoshi.jindanke.cn";
            showWVC.contentTitle = @"学习培训";
            [self.navigationController pushViewController:showWVC animated:YES];
            break;
        }
        case 7: {
            ShowWebViewController *showWVC = [[ShowWebViewController alloc] init];
            showWVC.showUrl = @"http://kaoshi.jindanke.cn/exam.html";
            showWVC.contentTitle = @"在线考试";
            [self.navigationController pushViewController:showWVC animated:YES];
            
            break;
        }
        case 8:
        {
            ShowWebViewController *showWVC = [[ShowWebViewController alloc] init];
            showWVC.showUrl = @"http://club.jindanke.cn";
            showWVC.contentTitle = @"互动社区";
            [self.navigationController pushViewController:showWVC animated:YES];
        }
            break;
        case 9:
        {
            AllInvoiceViewController *invoiceVC = [[AllInvoiceViewController alloc] init];
            invoiceVC.contentTitle = @"所有工作票";
            [self.navigationController pushViewController:invoiceVC animated:YES];
        }
            break;
        default:
            break;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
